package com.qf.program.service;

import cn.hutool.core.util.ZipUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import com.qf.common.utils.DateUtils;
import com.qf.utils.PubmedExtra;
import com.qf.utils.PubmedUtils2;
import com.qf.utils.SmallTool;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: gcr
 * @Date: 2022-3-30 15:46
 */
@Slf4j
public class 合并到本地256G {

    static String dbName2 = "xml_content_1_3qw";
    // 全部插入不判断 如果是true 则进行全部插入
    static boolean insert = false;
    //是不是要更新
    static boolean isupdate = true;
    //通过id进行开始


    @SneakyThrows
    public static void main(String[] args) {
        //  2021年11月18日15:41:15 插入2800万以后的数据,进行查询 没有的才插入,有就先不更新了
        //先删除 后插入

//        DSFactory.setCurrentDSFactory(new DruidDSFactory());
//        DataSource ds = DSFactory.get("server_local");
//        DataSource taiyids = DSFactory.get("taiyi");
        log.info("开始坐标++" + CreatePmidList.startIndex);
        int startPmid = CreatePmidList.startIndex * 1000;
        SmallTool.printMess("合并到本地256G++start+" + startPmid);
        Db use = Db.use();
        String allDate = "select max(pmid) as pmid  from " + dbName2;
        Entity query = use.query(allDate).get(0);
        SmallTool.printMess("最大的id 是" + query.getStr("pmid"));
        // 23057093 合适下这个pmid 是不是书籍 应该是了

        Integer count = query.getInt("pmid");
        int breakSize = 100000;
        int pageSize = count / breakSize + 1;
        SmallTool.printMess("全部页码+++++++++" + pageSize);

        SmallTool.printMess("任务开始+++++++++++++++++++++++++++++++++++++++++++++++++++++");
        for (int i = 1; i <= pageSize; i++) {

            int start = breakSize * (i - 1) + 1;
            int end = i * breakSize;
//            SmallTool.printMess("开始id++" + start + "结束id++++" + end);
            if (end <= startPmid) {
//                SmallTool.printMess("因为小于" + startPmid + " 略过了");
                continue;
            }
            String paramSql = "select * from " + dbName2 + " where  pmid between  " + start + " and " + end;
            List<Entity> querys = use.query(paramSql);
            SmallTool.printMess("当前页码+++++" + i);
            querys.parallelStream().forEach(entity -> {
//                Integer pmid = entity.getInt("pmid");

                String content = ZipUtil.unGzip(entity.getBytes("content"), "utf-8");
                //解析html
                Document doc = Jsoup.parse(content);
                try {
                    // 主要是用于进行过滤掉参文房子参文doi混入文章doi
                    doc.select("ReferenceList").remove();
                } catch (Exception e) {

                }
                List<Element> divList = new ArrayList<>();
                Elements pubmedArticles = doc.select("PubmedArticle");
                Elements pubmedBookArticles = doc.select("PubmedBookArticle");
                for (Element e : pubmedArticles) {
                    divList.add(e);
                }
                for (Element e : pubmedBookArticles) {
                    divList.add(e);
                }
                List<PubmedExtra> pubmeds = PubmedUtils2.parseHtml(divList);
                //直接入库
                for (PubmedExtra pubmed : pubmeds) {
                    if (pubmed.getPmid() <= startPmid) {
//                        SmallTool.printMess("小于开始 略过");
                        continue;
                    } else {
                        try {
                            insertData(pubmed, use);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
        }
        System.out.println("任务结束+++++++++++++++++++++++++++++++++++++++++++++++++++++");

    }

    @SneakyThrows
    public static void insertData(PubmedExtra pubmed, Db use) {
        if (insert) {
            use.insert(new Entity().setTableName(TableConfig.PMSPITABLEALLNEW)
                    .set("pmid", pubmed.getPmid())
                    .set("type", pubmed.getType())
                    .set("article_title", pubmed.getArticleTitle())
                    .set("source", pubmed.getSource())
                    .set("intro", pubmed.getIntro())
                    .set("jour", pubmed.getJour())
                    .set("volume", pubmed.getVolume())
                    .set("issue", pubmed.getIssue())
                    .set("pages", pubmed.getPages())
                    .set("vip", pubmed.getVip())
                    .set("pub_date", pubmed.getPubDate())
//                    .set("medline_date", pubmed.getMedlineDate())
                    .set("pub_year", pubmed.getPubYear())
                    .set("med_nlm_id", pubmed.getMedNlmId())
                    .set("author", pubmed.getAuthor())
                    .set("author_extend", pubmed.getAuthorExtend())
                    .set("affs", pubmed.getAffs())
                    .set("pmcid", pubmed.getPmcid())
                    .set("doi", pubmed.getDoi())
                    .set("lan", pubmed.getLan())
                    .set("ab_text", pubmed.getAbText())
                    .set("copyright", pubmed.getCopyright())
                    .set("keyword", pubmed.getKeyword())
                    .set("pts", pubmed.getPts())
                    .set("meshs", pubmed.getMeshs())
                    .set("mesh_heading", pubmed.getMeshHeading())
                    .set("mesh_headingy", pubmed.getMeshHeadingy())
                    .set("subs", pubmed.getSubs())
                    .set("grants", pubmed.getGrants())
                    .set("ass_date", pubmed.getAssDate())
                    .set("full_text", pubmed.getFullText())
                    .set("art_type", pubmed.getArtType())
            );
        } else {
            Entity info = null;
            if (!insert) {
                info = getInfo(use, pubmed.getPmid());
                if (info != null) {
                    if (isupdate) {
//                        SmallTool.printMess("更新了++" + pubmed.getPmid());
                        Entity delE = new Entity();
                        delE.setTableName(TableConfig.PMSPITABLEALLNEW);
                        delE.set("pmid", pubmed.getPmid());
                        Entity entity1 = new Entity();
                        entity1.set("pmid", pubmed.getPmid())
                                .set("type", pubmed.getType())
                                .set("article_title", pubmed.getArticleTitle())
                                .set("source", pubmed.getSource())
                                .set("intro", pubmed.getIntro())
                                .set("jour", pubmed.getJour())
                                .set("volume", pubmed.getVolume())
                                .set("issue", pubmed.getIssue())
                                .set("pages", pubmed.getPages())
                                .set("vip", pubmed.getVip())
                                .set("pub_date", pubmed.getPubDate())
//                                .set("medline_date", pubmed.getMedlineDate())
                                .set("pub_year", pubmed.getPubYear())
                                .set("med_nlm_id", pubmed.getMedNlmId())
                                .set("author", pubmed.getAuthor())
                                .set("author_extend", pubmed.getAuthorExtend())
                                .set("affs", pubmed.getAffs())
                                .set("pmcid", pubmed.getPmcid())
                                .set("doi", pubmed.getDoi())
                                .set("lan", pubmed.getLan())
                                .set("ab_text", pubmed.getAbText())
                                .set("copyright", pubmed.getCopyright())
                                .set("keyword", pubmed.getKeyword())
                                .set("pts", pubmed.getPts())
                                .set("meshs", pubmed.getMeshs())
                                .set("mesh_heading", pubmed.getMeshHeading())
                                .set("mesh_headingy", pubmed.getMeshHeadingy())
                                .set("subs", pubmed.getSubs())
                                .set("grants", pubmed.getGrants())
                                .set("ass_date", pubmed.getAssDate())
                                .set("full_text", pubmed.getFullText())
                                .set("art_type", pubmed.getArtType())
                                .set("last_date", DateUtils.parseLong(System.currentTimeMillis()));
                        use.update(entity1, delE);
                    }

                } else {
                    use.insert(new Entity().setTableName(TableConfig.PMSPITABLEALLNEW)
                            .set("pmid", pubmed.getPmid())
                            .set("type", pubmed.getType())
                            .set("article_title", pubmed.getArticleTitle())
                            .set("source", pubmed.getSource())
                            .set("intro", pubmed.getIntro())
                            .set("jour", pubmed.getJour())
                            .set("volume", pubmed.getVolume())
                            .set("issue", pubmed.getIssue())
                            .set("pages", pubmed.getPages())
                            .set("vip", pubmed.getVip())
                            .set("pub_date", pubmed.getPubDate())
//                            .set("medline_date", pubmed.getMedlineDate())
                            .set("pub_year", pubmed.getPubYear())
                            .set("med_nlm_id", pubmed.getMedNlmId())
                            .set("author", pubmed.getAuthor())
                            .set("author_extend", pubmed.getAuthorExtend())
                            .set("affs", pubmed.getAffs())
                            .set("pmcid", pubmed.getPmcid())
                            .set("doi", pubmed.getDoi())
                            .set("lan", pubmed.getLan())
                            .set("ab_text", pubmed.getAbText())
                            .set("copyright", pubmed.getCopyright())
                            .set("keyword", pubmed.getKeyword())
                            .set("pts", pubmed.getPts())
                            .set("meshs", pubmed.getMeshs())
                            .set("mesh_heading", pubmed.getMeshHeading())
                            .set("mesh_headingy", pubmed.getMeshHeadingy())
                            .set("subs", pubmed.getSubs())
                            .set("grants", pubmed.getGrants())
                            .set("ass_date", pubmed.getAssDate())
                            .set("full_text", pubmed.getFullText())
                            .set("art_type", pubmed.getArtType())
                    );

                }
            }


        }
    }

    @SneakyThrows
    public static Entity getInfo(Db use, Integer pmid) {
        String sqlParam = "select pmid from " + TableConfig.PMSPITABLEALLNEW + " where pmid = ?";
        Entity entity = use.queryOne(sqlParam, pmid);
        return entity;
    }

}
